home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright (C) 1992 by Mark W. Schumann
-
- convtext.cpp -- Convert a text file into
- one or more existing Clipper files under
- marker-file control.
-
- Compiler: Borland C++ 2.0.
- Library: SoftC version 3.0.
-
- 20 April 1992, by Mark W. Schumann
- Usenet: mark@whizbang.ncoast.org
-
- Compile (Borland) with:
-
- bcc -ms -lm convtext.cpp scdbp20s.lib
- */
-
-
- #include <softc.h>
- #include <sc_base.h>
- #include <fstream.h>
- #include <iostream.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
-
- #define LINELENGTH 200
-
- int convtext (char *);
- char *strtrim (char *);
- int nextmark (int, long &, char *, char *);
-
- int main (int argc, char *argv[])
-
- {
-
- scdinit (5, 0);
- convtext ("POLLSEND.TXT");
- scdterm();
-
- }
-
- int convtext (char *textfile)
-
- {
-
- ifstream ip; // Text input stream.
-
- char tempname[80]; // Builds file name.
- char s[LINELENGTH+2]; // Text record buffer.
- char *p; // Throwaway pointer.
-
- int dbf = -1; // Output database handle.
- long recno; // Current record number.
-
- int markdbf; // Marker database handle.
- char marktext[30]; // Text to indicate mark record.
- char markfile[9]; // Output file indicated by mark.
- long markrec = 0L; // Current record in marker file.
- int marklen; // Length of marker for comparison.
-
- s[0] = ' '; // Record status byte.
-
- ip.open (textfile);
-
- if (!ip) {
- cout << "Couldn't open " << textfile << ".\n";
- return -1;
- }
-
- scddopenx (&markdbf, "DATAMARK.DBF", 0);
- if (scecode() < 0) {
- cout << "Couldn't open marker file. "
- << "SoftC error was " << scecode() << '\n';
- return -1;
- }
-
- nextmark (markdbf, markrec, marktext, markfile);
- strtrim (marktext);
- marklen = strlen (marktext);
-
- while (ip.getline (&s[1], LINELENGTH)) {
-
- sceclr();
-
- // Trim trailing newline.
- p = strchr (&s[1], '\n');
- if (p != NULL) *p = '\0';
-
- // Next data marker record reached?
- if (strncmp (marktext, &s[1], marklen) == 0) {
-
- // Close current output database.
- if (dbf != -1) scddclose (dbf);
-
- // Build full name of the target file.
- strcpy (tempname, markfile);
- strtrim (tempname);
- strcat (tempname, ".DBF");
-
- // Now open it.
- cout << "\nOpening " << markfile << '\n';
- scddopenx (&dbf, tempname,
- SC_RDWR | SC_EXCLUDE);
-
- scddsize (dbf, &recno);
-
- // Use the next marker.
- nextmark (markdbf, markrec,
- marktext, markfile);
-
- if (scecode() < 0) {
- cout << "Error opening " <<
- tempname << ". SoftC error was " <<
- scecode() << '\n';
- break;
- }
-
- }
-
- else {
-
- if (dbf == -1) {
- cout << "Input file doesn't begin with a" <<
- " data marker.\n";
- break;
- }
-
- recno++;
-
- // Write the record to the database.
- if (scddrputx (dbf, s, &recno, SC_ADD) < 0) {
- cout << "Couldn't add record " << recno
- << "; SoftC error " << scecode()
- << ' ' << scemsg() << '\n';
- break;
- }
- else
- cout << "Record number " << recno
- << " added.\r";
-
- }
- }
-
- cout << '\n';
- ip.close();
- scddclose (markdbf);
- if (dbf != -1) scddclose (dbf);
- return 0;
-
- }
-
-
- // Remove trailing whitespace from a string.
-
- char *strtrim (char *s)
-
- {
-
- register char *p;
- char *q = NULL; // Last printing character.
-
- for (p = s; *p; p++)
- if (!isspace (*p)) q = p;
-
- if (q != NULL)
- *++q = '\0';
-
- return s;
- }
-
-
- // This assumes that we have a marker text description
- // in field 1 and marker file name in field 2. It is also
- // counting on character buffers long enough to hold the
- // results.
-
- int nextmark (int dbf, long &rec, char *text, char *file)
-
- {
-
- scddrget (dbf, ++rec);
- scddfget (dbf, 1, text);
- scddfget (dbf, 2, file);
- return scecode();
-
- }
-